﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace GoogleChartSharp.CustomCharts
{
    /// <summary>
    /// 
    /// </summary>
    public class LabeledValuePieChart : PieChart
    {
        string[] originalDataLabels;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public LabeledValuePieChart(int width, int height)
            : base(width, height)
        { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="pieChartType"></param>
        public LabeledValuePieChart(int width, int height, PieChartType pieChartType)
            : base(width, height, pieChartType)
        { }

        #region Overriden SetData methods
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataToShow"></param>
        public override void SetData(float[] dataToShow)
        {
            StoreDataLabels(dataToShow);
            base.SetData(dataToShow);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataToShow"></param>
        /// <param name="maxValueToScale"></param>
        public override void SetData(float[] dataToShow, int maxValueToScale)
        {
            StoreDataLabels(dataToShow);
            base.SetData(dataToShow, maxValueToScale);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataToShow"></param>
        public override void SetData(int[] dataToShow)
        {
            StoreDataLabels(dataToShow);
            base.SetData(dataToShow);
        }
        #endregion

        private void StoreDataLabels(float[] data)
        {
            originalDataLabels = new string[data.Length];
            for (int i = 0; i < data.Length;i++ )
            {
                originalDataLabels[i] = data[i].ToString(CultureInfo.InvariantCulture);
            }
        }

        private void StoreDataLabels(int[] data)
        {
            originalDataLabels = new string[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                originalDataLabels[i] = data[i].ToString(CultureInfo.InvariantCulture);
            }
        }

        /// <summary>
        /// Collects the chl=xxx element to include chart labels.
        /// </summary>
        /// <returns> The entire chl element to be included in the url.</returns>
        protected override string CollectChartLabelElement()
        {
            string s = "chl=";
            for (int i=0;i<pieChartLabels.Length;i++)
            {
                s += Uri.EscapeDataString(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", pieChartLabels[i], originalDataLabels[i])) + "|";
            }
            return s.TrimEnd("|".ToCharArray());
        }

    }
}
